﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PhoneBookApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private int _currentContactId;

        private void buttonAddContakt_Click(object sender, EventArgs e)
        {
            if (textBoxName.Text != "" && textBoxLastName.Text != "")
            {
                DataRow row = dataTableContacts.NewRow();
                row["Name"] = textBoxName.Text;
                row["Last Name"] = textBoxLastName.Text;
                dataTableContacts.Rows.Add(row);

                listUpdate();
                phoneUpdate();
            }
        }

        private void listView_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int _currentRowIndex = listViewContacts.SelectedItems[0].Index;

                DataRow row = dataTableContacts.Rows[_currentRowIndex];
                textBoxName.Text = row["Name"].ToString();
                textBoxLastName.Text = row["Last Name"].ToString();
                _currentContactId = (int)row["id"];
                labelCurentContact.Text = textBoxName.Text + " " + textBoxLastName.Text;
            }
            catch(Exception ex)
            {
          
            }

            phoneUpdate();
        }

        private void buttonChangeContact_Click(object sender, EventArgs e)
        {
            if (textBoxName.Text != "" && textBoxLastName.Text != "")
            {
                string _filter = "id='" + _currentContactId + "'";
                DataRow[] _contactRows = dataTableContacts.Select(_filter);

                _contactRows[0].BeginEdit();
                _contactRows[0]["Name"] = textBoxName.Text;
                _contactRows[0]["Last Name"] = textBoxLastName.Text;
                _contactRows[0].EndEdit();

                dataTableContacts.AcceptChanges();
                listUpdate();
            }
        }

        private void buttonRemoveContact_Click(object sender, EventArgs e)
        {
            try
            {
                string _filter = "ContactsId='" +
                    listViewContacts.SelectedItems[0].Tag.ToString() + "'";
                DataRow[] phones = dataTablePhones.Select(_filter);

                for (int i = 0; i < phones.Length; i++)
                {
                    if (phones.Length != 0)
                    {
                        phones[i].Delete();
                    }
                }
                dataTablePhones.AcceptChanges();

                dataTableContacts.Rows[
                    listViewContacts.SelectedItems[0].Index].Delete();

                dataTableContacts.AcceptChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            listUpdate();
            phoneUpdate();
        }

        private void buttonAddPhone_Click(object sender, EventArgs e)
        {
            if (textBoxNumber.Text != "")
            {
                try
                {
                    DataRow row = dataTablePhones.NewRow();
                    row["ContactsId"] = _currentContactId;
                    row["Number"] = textBoxNumber.Text;
                    dataTablePhones.Rows.Add(row);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            phoneUpdate();
        }

        private void buttonRmovePhone_Click(object sender, EventArgs e)
        {
            try
            {
                string _filter = "ContactsId='" +
                    listViewContacts.SelectedItems[0].Tag.ToString() + "'";
                DataRow[] phones = dataTablePhones.Select(_filter);

                for (int i = 0; i < phones.Length; i++)
                {
                    if (phones.Length != 0)
                    {
                        phones[i].Delete();
                    }
                }
                dataTablePhones.AcceptChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            phoneUpdate();
        }

        private void buttonCnangePhone_Click(object sender, EventArgs e)
        {
            if (textBoxNumber.Text != "")
            {
                string _filter = "id='" + 
                    listView2.SelectedItems[0].Tag.ToString() + "'";
                DataRow[] _phonesRows = dataTablePhones.Select(_filter);

                _phonesRows[0].BeginEdit();
                _phonesRows[0]["Number"] = textBoxNumber.Text;
                _phonesRows[0].EndEdit();

                dataTableContacts.AcceptChanges();
                phoneUpdate();
            }
        }

        public void listUpdate()
        {
            listViewContacts.Items.Clear();

            foreach (DataRow row in dataTableContacts.Rows)
            {
                ListViewItem _lvi = new ListViewItem(row["Name"].ToString());
                _lvi.SubItems.Add(row["Last Name"].ToString());
                _lvi.SubItems.Add(row["Id"].ToString());
                _lvi.Tag = row["Id"].ToString();
                listViewContacts.Items.Add(_lvi );
            }
        }

        public void phoneUpdate()
        {
            listView2.Items.Clear();

            string _filter = 
                "ContactsId='"+_currentContactId.ToString()+"'";

            DataRow[] phones = dataTablePhones.Select(_filter);

            if (phones.Length != 0)
            {
                for (int i = 0; i < phones.Length; i++)
                {
                    ListViewItem _lvi = new ListViewItem(
                        phones[i]["Number"].ToString());
                    _lvi.SubItems.Add(phones[i]["Id"].ToString());
                    _lvi.SubItems.Add(phones[i]["ContactsId"].ToString());
                    _lvi.Tag = phones[i]["Id"];

                    listView2.Items.Add(_lvi);
                }
            }
        }
    }
}
